﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MovieCollectionManager.DataModel;

namespace MovieCollectionManager
{
    public partial class DeleteUserForm : Form
    {
        public DeleteUserForm()
        {
            InitializeComponent();
        }

        private void DeleteUserForm_Load(object sender, EventArgs e)
        {
            BindUsers();
        }

        private void BindUsers()
        {
            using (var db = new MovieCollectionConteiner())
            {
                List<User> users = db.Users.ToList();
                comboBoxUserList.DataSource = users;
                comboBoxUserList.DisplayMember = "Name";
                comboBoxUserList.ValueMember = "Id";
            }
        }

        private void buttonRemoveUser_Click(object sender, EventArgs e)
        {
            if (comboBoxUserList.Items.Count == 0)
                return;

            if (MessageBox.Show("Deseja realmente remover o usuário selecionado?", "Confirmar remoção", MessageBoxButtons.YesNo) == DialogResult.No)
                return;

            using (var db = new MovieCollectionConteiner())
            {
                User user = (from user1 in db.Users
                             where user1.Id == (int) comboBoxUserList.SelectedValue
                             select user1).First();
                db.DeleteObject(user);
                db.SaveChanges();
            }
            BindUsers();
            MessageBox.Show("Usuário removido!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void buttonBack_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
